﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using EbbinghausWord.BLL;
using EbbinghausWord.Common;
using EbbinghausWord.Entities;
using System.Linq;

namespace EbbinghausWord.DAL
{
    /// <summary>
    /// 书籍管理
    /// </summary>
    public class BookDAL
    {
        #region private field
        private static readonly List<BookEntity> _allBooks = new List<BookEntity>()
                       {
                           new BookEntity(){BookId = 1, BookName = "中考词汇"},
                           new BookEntity(){BookId = 2, BookName = "高考词汇"},
                           new BookEntity(){BookId = 4, BookName = "全国大学英语四级"},
                           new BookEntity(){BookId = 6, BookName = "全国大学英语六级"},
                           new BookEntity(){BookId = 7, BookName = "研究生入学考试"},
                           new BookEntity(){BookId = 21, BookName = "TOFEL"},
                           new BookEntity(){BookId = 22, BookName = "IELTS"},
                           new BookEntity(){BookId = 23, BookName = "GRE"},
                           new BookEntity(){BookId = 11, BookName = "新概念英语1"},
                           new BookEntity(){BookId = 12, BookName = "新概念英语2"},
                           new BookEntity(){BookId = 13, BookName = "新概念英语3"},
                           new BookEntity(){BookId = 14, BookName = "新概念英语4"},
                       };

        private static readonly SysDataContext _sysDb = new SysDataContext();
        private static List<TableBookWordEntity> _currentBookWords = null;
        private static List<TableBookWordEntity> _allBookWords = null;
        #endregion

        #region public method

        /// <summary>
        /// 初始化书籍-单词库
        /// </summary>
        public static void InitCurrentBookWord()
        {
            _currentBookWords = new List<TableBookWordEntity>();

            //是否有BookId，如果有，则加载_bookWords，否则不加载
            if (new UserConfig().UserBookId != 0)
            {
                //打开词典文件
                var works = XElement.Load(@"words/relation/" + new UserConfig().UserBookId.ToString() + ".xml");

                //初始化词典
                foreach (var element in works.Elements())
                {
                    _currentBookWords.Add(TableBookWordEntity.FromXElemnt(element, new UserConfig().UserBookId));
                }
            }
        }

        /// <summary>
        /// 获得所有词典单词对应关系
        /// </summary>
        public static List<TableBookWordEntity> GetAllBookWords()
        {
            if (_allBookWords == null)
            {
                _allBookWords = new List<TableBookWordEntity>();

                _allBooks.ForEach(x =>
                                      {
                                          //打开词典文件
                                          var works = XElement.Load(@"words/relation/" + x.BookId + ".xml");

                                          //初始化词典
                                          foreach (var element in works.Elements())
                                          {
                                              _allBookWords.Add(TableBookWordEntity.FromXElemnt(element, x.BookId));
                                          }
                                      });
            }

            return _allBookWords;
        }


        /// <summary>
        /// 获得所有图书
        /// </summary>
        public static List<BookEntity> GetAllBook()
        {
            return _allBooks;
        }

        /// <summary>
        /// 获得图书详细信息
        /// </summary>
        /// <returns></returns>
        public static List<BookEntity> GetAllBookDetails()
        {
            var et = new ExecuteTimer(false);

            //获得用户单词数据
            var userWords = _sysDb.TableUserWordEntities.Select(x => new { x.English, x.IsInTheWordBook, x.RememberLevel }).ToList();
            et.Show("GetAllUserWords");

            var bookWordCounts = GetAllBookWords().GroupBy(x => x.BookId).Select(x => new { BookId = x.Key, WordCount = x.Count() });

            //获得学习中的单词数
            var learningBookWords = GetAllBookWords()
                .Join(userWords, x => x.English, x => x.English,
                      (x, y) => new
                                    {
                                        x.BookId,
                                        y.IsInTheWordBook,
                                        RemeberLevel = y.RememberLevel,
                                    });

            var achievementRememberLevel = SystemConfig.AchievementRememberLevel;

            var books = bookWordCounts.Join(_allBooks, x => x.BookId, x => x.BookId,
            (x, y) => new BookEntity()
            {
                BookId = x.BookId,
                BookName = y.BookName,
                WordCount = x.WordCount,
            })
            .GroupJoin(learningBookWords, x => x.BookId, x => x.BookId, (x, y) =>
                new BookEntity()
                    {
                        BookId = x.BookId,
                        BookName = x.BookName,
                        WordCount = x.WordCount,
                        LearningWordCount = y.Count(z => z.IsInTheWordBook && z.RemeberLevel != achievementRememberLevel),
                        LearnedWordCount = y.Count(z => z.RemeberLevel == achievementRememberLevel),
                    })
            .ToList();
            et.Show("Query");

            return books;
        }

        /// <summary>
        /// 根据当前书籍Id，获得该书籍对应的单词英文列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetCurrentBookEnglishList()
        {
            //var userBookId = new UserConfig().UserBookId;
            //return _currentBookWords.Where(x => x.BookId == userBookId)
            //    .Select(x => x.English).ToList();

            return _currentBookWords.Select(x => x.English).ToList();
        }

        /// <summary>
        /// 获得当前书籍对应的单词数量
        /// </summary>
        /// <returns></returns>
        public int GetCurrentBookWordCount()
        {
            return _currentBookWords.Count();// (x => x.BookId == new UserConfig().UserBookId);
        }

        /// <summary>
        /// 根据书籍Id获得对应单词数量
        /// </summary>
        /// <returns></returns>
        public int GetBookWordCountByBookId(int bookId)
        {
            return GetAllBookWords().Count(x => x.BookId == bookId);
        }

        #endregion
    }
}
